package com.example.administrator.boltshow.viewone;/*
 *@创建者 lnn
 *@创建时间2016/8/2521:41
 *@描述 ${TODO}
*/

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

public class FlowLayout extends FrameLayout {
    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //这个方法是用来摆放子控件位置的方法
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int width = getWidth();
        int childCount = getChildCount();
        int childLeft = 10;
        int childTop = 10;
        for (int i = 0; i < childCount; i++) {
            //获取当前循环的子控件
            View childAt = getChildAt(i);
            //控件的右边 应该是 控件的左边加上控件的宽度
            int childRight = childLeft + childAt.getMeasuredWidth();
            //控件的bottmo 应该是控件的顶部加上控件的高度
            int childBottom = childTop + childAt.getMeasuredHeight();

            if (width < childRight) {
                childLeft = 10;
                childTop = childBottom + 10;
                childRight = childLeft + childAt.getMeasuredWidth();
                childBottom = childTop + childAt.getMeasuredHeight();

            }
            childAt.layout(childLeft, childTop, childRight, childBottom);
            childLeft = childRight + 10;

        }

    }


}
